package com.hilinos.common.util;

/**
 * 进制转换工具
 */
public class RadixUtils {

    private static final char[] RADIX62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    private static final char[] RADIX36 = "0123456789abcdefghijklmnopqrstuvwxyz".toCharArray();

    /**
     * 转换为64进制
     *
     * @param num
     * @return
     */
    public static String radix62(long num) {
        return radix(num, RADIX62);
    }

    /**
     * 转换为36进制
     *
     * @param num
     * @return
     */
    public static String radix36(long num) {
        return radix(num, RADIX36);
    }

    /**
     * 转换为16进制
     *
     * @param num
     * @return
     */
    public static String radix16(long num) {
        return Long.toHexString(num);
    }

    /**
     * 转换为8进制
     *
     * @param num
     * @return
     */
    public static String radix8(long num) {
        return Long.toOctalString(num);
    }


    /**
     * 转换为2进制
     *
     * @param num
     * @return
     */
    public static String radix2(long num) {
        return Long.toBinaryString(num);
    }

    /**
     * 任意进制转换
     *
     * @param num
     * @param radixCharArr 进制对应的字符
     * @return
     */
    public static String radix(long num, char[] radixCharArr) {
        StringBuilder sb = new StringBuilder();
        while (num > 0) {
            sb.insert(0, radixCharArr[(int) (num % radixCharArr.length)]);
            num = num / radixCharArr.length;
        }
        return sb.toString();
    }

}
